<template>
  <div id="Tea">
    <el-breadcrumb
      separator="/"
      style="padding-left: 10px; padding-bottom: 10px; font-size: 12px"
    >
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>教师管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card" shadow="hover">
      <el-form
        :inline="true"
        :model="queryMap"
        size="small"
        class="demo-form-inline"
      >
        <el-form-item label="姓名">
          <el-input
            v-model="queryMap.teaName"
            placeholder="请输入姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="职工号">
          <el-input
            v-model="queryMap.teaNum"
            placeholder="请输入职工号"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="search"
            type="primary"
            icon="el-icon-search"
            :loading="loading"
            round
            >查询</el-button
          >
          <el-button
            type="success"
            icon="el-icon-plus"
            round
            :loading="loading"
            @click="(show = true), (currentTea = {})"
            >新增</el-button
          >
        </el-form-item>
      </el-form>
      <template>
        <el-table
          :data="teaList"
          border
          stripe
          size="mini"
          style="width: 100%; margin-top: 5px"
        >
          <el-table-column
            prop="teaNum"
            label="职工号"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="teaName"
            label="姓名"
            align="center"
          ></el-table-column>
          <el-table-column label="是否可用" align="center">
            <template slot-scope="scope">
              <el-switch v-model="scope.row.status" disabled></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200" align="center">
            <template slot-scope="scope">
              <el-button
                type="text"
                icon="el-icon-edit"
                size="mini"
                :loading="loading"
                @click="getCurrentTea(scope.row.teaId)"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 分页 -->
      <el-pagination
        style="margin-top: 10px"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.queryMap.page"
        :page-sizes="[10, 50, 100]"
        :page-size="this.queryMap.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <el-dialog title="教职工详情信息" :visible.sync="show" width="400px">
      <el-form :model="currentTea" align="center">
        <el-form-item label="职工号" label-width="70px">
          <el-input
            v-model="currentTea.num"
            autocomplete="off"
            style="width: 220px"
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名" label-width="70px">
          <el-input
            v-model="currentTea.username"
            autocomplete="off"
            style="width: 220px"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="70px">
          <el-input
            v-model="currentTea.password"
            autocomplete="off"
            style="width: 220px"
            showPassword
          ></el-input>
        </el-form-item>
        <el-form-item label="是否可用" label-width="70px">
          <el-select v-model="currentTea.status" placeholder="请选择是否可用">
            <el-option :key="true" label="可用" :value="true"></el-option>
            <el-option :key="false" label="不可用" :value="false"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色授权" label-width="70px">
          <el-select v-model="currentTea.roleIds" multiple placeholder="请选择">
            <el-option
              v-for="item in roleList"
              :key="item.roleId"
              :label="item.roleDes"
              :value="item.roleId"
              :disabled="item.roleId==6"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          :loading="loading"
          @click="updateCurrentTea(currentTea.userId)"
          >确 定</el-button
        >
        <el-button @click="show = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapState } from "vuex";
import {
  queryTeasByPage,
  queryCurrentTea,
  saveCurrentTea,
  editCurrentTea,
  queryRolesByPage,
} from "api/api";
export default {
  data() {
    return {
      queryMap: { page: 1, size: 10 },
      loading: false,
      show: false,
      teaList: [],
      total: 0,
      currentTea: {},
      roleList: [],
    };
  },
  computed: {
    ...mapState(["userInfo"]),
  },
  methods: {
    //改变页码
    handleSizeChange(newSize) {
      this.queryMap.size = newSize;
      this.getTeasByPage();
    },
    //翻页
    handleCurrentChange(current) {
      this.queryMap.page = current;
      this.getTeasByPage();
    },
    //条件查询
    getTeasByPage() {
      queryTeasByPage(this.userInfo.sessionId, this.queryMap).then((res) => {
        if (!res.flag) {
          this.$message.error(res.message);
          this.total = 0;
          this.teaList = [];
        } else {
          this.total = res.data.total;
          this.teaList = res.data.rows;
        }
      });
    },
    search() {
      queryTeasByPage(this.userInfo.sessionId, this.queryMap).then((res) => {
        if (!res.flag) {
          this.$message.error(res.message);
        } else {
          this.total = res.data.total;
          this.teaList = res.data.rows;
        }
      });
    },
    //获取当前教师详情信息
    getCurrentTea(teaId) {
      this.show = true;
      queryCurrentTea(this.userInfo.sessionId, teaId).then((res) => {
        if (!res.flag) {
          this.$message.error(res.message);
        } else {
          this.currentTea = res.data;
          if (
            this.currentTea.roleIds == null ||
            this.currentTea.roleIds == []
          ) {
            this.$message({
              type: "warning",
              message: "当前用户未进行角色授权，请及时授权，以便后续使用",
            });
          }
        }
      });
    },
    //更新信息
    async updateCurrentTea(teaId) {
      this.loading = true;
      if (teaId != null) {
        //更新操作
        await editCurrentTea(this.userInfo.sessionId, this.currentTea).then(
          (res) => {
            if (!res.flag) {
              this.$message.error(res.message);
              this.loading = false;
            } else {
              this.$message({
                type: "success",
                message: "更新成功！",
              });
              this.loading = false;
              this.show = false;
              this.getTeasByPage();
            }
          }
        );
      } else {
        //新增操作
        await saveCurrentTea(this.userInfo.sessionId, this.currentTea).then(
          (res) => {
            if (!res.flag) {
              this.$message.error(res.message);
              this.loading = false;
            } else {
              this.$message({
                type: "success",
                message: "新增成功！",
              });
              this.loading = false;
              this.show = false;
              this.getTeasByPage();
            }
          }
        );
      }
    },
    //条件查询
    getRolesByPage() {
      queryRolesByPage(this.userInfo.sessionId, { page: 1, size: 100 }).then(
        (res) => {
          if (!res.flag) {
            this.$message.error(res.message);
            this.roleList = [];
          } else {
            this.roleList = res.data.rows;
          }
        }
      );
    },
  },
  mounted() {
    this.getTeasByPage();
    this.getRolesByPage();
  },
};
</script>